package com.aletto.general_store.utils;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class ScoreProgress extends View {

    private double percentage;
    private final Paint backgroundPaint, paint;

    public ScoreProgress(Context context) {
        this(context, null);
    }

    public ScoreProgress(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ScoreProgress(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        backgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        backgroundPaint.setColor(Color.parseColor("#F5F7FA"));

        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.parseColor("#848E9C"));
    }

    @Override
    protected void onDraw(@NonNull Canvas canvas) {
        super.onDraw(canvas);

        // 背景
        canvas.drawRoundRect(
                0, 0, getWidth(), getHeight(),
                dp2px(4),
                dp2px(4),
                backgroundPaint
        );

        // 进度
        canvas.drawRoundRect(
                0, 0, (int)(getWidth() * percentage), getHeight(),
                dp2px(4),
                dp2px(4),
                paint
        );
    }

    public void setPercentage(double percentage){
        this.percentage = percentage;
        invalidate();
    }

    private int dp2px(float value) {
        return (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, getResources().getDisplayMetrics()) + 0.5f);
    }

}
